package com.zjy.pgcms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjy.pgcms.common.ErrorCode;
import com.zjy.pgcms.dto.user.UserQueryDTO;
import com.zjy.pgcms.dto.user.UserQueryPage;
import com.zjy.pgcms.entity.User;
import com.zjy.pgcms.exception.BusinessException;
import com.zjy.pgcms.mapper.UserMapper;
import com.zjy.pgcms.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 音音音
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {
    @Value("${server.port}")
    private int port;

    @Resource
    private UserMapper userMapper;

    @Override
    public String saveAvatar(MultipartFile file, Long userID) {
        try {
            String basePath = "E:\\project\\pgcms\\src\\main\\resources\\static";
            String separator = System.getProperty("file.separator");
            String avatarDir = "upload"+separator+"avatar";
            String fileName = userID+"_"+file.getOriginalFilename();
            String filePath = basePath + separator + avatarDir + separator + fileName;
            File avatarFile = new File(filePath);
            if (!avatarFile.getParentFile().exists()){
                avatarFile.getParentFile().mkdirs();
            }
            file.transferTo(avatarFile);
            String hostAddress = "";
            try {
                 hostAddress = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            return "http://"+hostAddress + ":"+ port + "/upload/avatar/" +fileName;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public QueryWrapper getQueryWrapper(UserQueryDTO dto) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (dto.getId()!=null){
            if (StringUtils.isNotBlank(dto.getId().toString())){
                queryWrapper.like("id",dto.getId());
            }
        }
        if (StringUtils.isNotBlank(dto.getUsername())){
            queryWrapper.like("username",dto.getUsername());
        }
        if (dto.getGender()!=null){
            if (StringUtils.isNotBlank(dto.getGender().toString())){
                queryWrapper.eq("gender",dto.getGender());
            }
        }
        if (StringUtils.isNotBlank(dto.getName())){
            queryWrapper.like("name",dto.getName());
        }
        if (StringUtils.isNotBlank(dto.getEmail())){
            queryWrapper.like("email",dto.getEmail());
        }
        if (StringUtils.isNotBlank(dto.getPhone())){
            queryWrapper.like("phone",dto.getPhone());
        }
        return queryWrapper;
    }

    @Override
    public void userExist(String name, String password) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",name);
        queryWrapper.eq("password",password);
        User user = this.getOne(queryWrapper);
        if (user == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户不存在");
        }
    }

    @Override
    public List<User> queryUserByPage(UserQueryPage params) {
        Map<String, Object> map = new HashMap<>();
        if (params.getId() != null && params.getId().toString().length() != 0 ){
            map.put("id",params.getId());
        }
        if (params.getUsername() != null && params.getUsername().length() != 0 ){
            map.put("username",params.getUsername());
        }
        if (params.getPhone() != null && params.getPhone().length() != 0 ){
            map.put("phone",params.getPhone());
        }
        map.put("page",(params.getPage() -1)*params.getSize());
        map.put("size",params.getSize());
        return userMapper.selectByPage(map);
    }

    @Override
    public long count(UserQueryPage params) {
        Map<String, Object> map = new HashMap<>();
        if (params.getId() != null && params.getId().toString().length() != 0 ){
            map.put("id",params.getId());
        }
        if (params.getUsername() != null && params.getUsername().length() != 0 ){
            map.put("username",params.getUsername());
        }
        if (params.getPhone() != null && params.getPhone().length() != 0 ){
            map.put("phone",params.getPhone());
        }
        return userMapper.userCount(map);
    }
}